import { getAuthentication, getEncryptedParam, getToken } from "@/utils/auth";


export const hostName = '818ps.com';
export function ipsApi(apiPath: string) { return '//' + hostName + apiPath; }
const options: { credentials: 'include' } = {
    credentials: 'include'
};
// 获取code 保证安全
export const getTelCodeToken = (sms_key: string) => {
    return fetch(`/site-api/send-tel-login-code-token?sms_key=${sms_key}`, options)
}
export const sendLoginValidateInfo = (param: { phoneNum: string; token: string }) => {
    return fetch(`/site-api/send-tel-login-code?num=${param.phoneNum}&sms_token=${param.token}`, options)
}
/**
 * 点击手机登录
 * @param {*} param
 */
export const sendLoginPanelIn = (param: { phoneNum: string, phoneMsgNum: string }) => {
    const { phoneNum, phoneMsgNum } = param
    let postData = `telCode=${phoneMsgNum}&telNum=${phoneNum}`
    return fetch(`/site/tel-login-callback?isAjax=1`, {
        method: 'POST',
        body: postData,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        ...options
    })
}
/**
 * 编辑器内登录获取图片验证码
 * @param {*} param
 */
export const checkValidateTimes = (param: { ticket: string; rendstr: string; tel: string }) => {
    return fetch(`/validate/check-tel-hua-kuai?ticket=${param.ticket}&rendstr=${param.rendstr}&tel=${param.tel}`, options)
}
/**
 * 编辑器内登录获取图片验证码
 */
export const getLoginValidateImg = () => {
    return fetch('/site-api/validate-code-img', options)
}

/**
 * 微信登录检测
 */
export const wxLoginCheck = (wechatNumber:string) => { 
    return fetch('/site-api/check-wechat-login-qrcode', {
        method: 'POST',
        body: 'number=' + wechatNumber,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        credentials: 'include'
    }).then(res=>res.json())
}

/**
 * 微信登录
 */
export const wxLogin = (wechatNumber:string) => { 
    return fetch('/site/wechat-official-login?authclient=wechat&isAjax=1&scope=snsapi_login&number=' + wechatNumber + `&login_page=${false ? 'win_ue&isClientSide=1' : 'ue'}` + '&refer=' + '&auto_login=', options).then(res=>res.json())
}
export const getUserInfo = () => { 
    const encryptionObj = getEncryptedParam(getToken(), {})
      return fetch('/api/getuserinfo' + `?${encryptionObj.param}`, {
        method: 'POST',
        credentials: 'include'
      }).then(res=>res.json())
}

